var msgset = "";
//var searchname = {{.T.searchname}}
//var s_type = {{.T.s_type}}
var s_type = "";
var s_keywords = "";
var wochat = "";
var jyemail = "";
var dataObj = {
    reqType: "other",
    keys: "",
    scope: "",
    mode: "",
    s_email: ""
};
$(function () {
    winWidth = $(window).width();
    winHeight = $(window).height();
    $(".jy_edit span").css("max-width", winWidth * 0.48);
    $(".jy_rssetbj").css("height", winWidth * 0.36);
    $(".modal-dialog").css("margin-top", winHeight * 0.25);
    var setKeyWord = function (type, keys) {
        for (var k in keys) {
            k = keys.length - 1;
            if (keys[k].replace(/\s/g, "").length == 0) {
                $(".child-node").find("li:eq(0)").append('<div class="jycom-last">点击订阅关键词</div>');
                return;
            }
        }
        if (typeof(keys) == "undefined" || keys.length < 1) {
            $(".child-node").find("li:eq(0)").append('<div class="jycom-last">点击订阅关键词</div>');
            return;
        }
        var skey = []
        for (var i = 0; i < keys.length; i++) {
            skey.push(keys[i].replace(/\+/g, " "));
        }
        $(".jycom-last").remove();
        $("#" + type + "-content li:first").append('<div class="com-last">' + skey.join(";<br>") + '</div>');
    };
    var setScope = function (type, scope) {
        var dialogObj = $("#" + type + "-scope");
        if (typeof(scope) == "undefined" || scope == "") {
            $(".child-node").find("li:eq(1)").append('<div class="jycom-last">全国</div>');
            return;
        }
        if (scope == "A") {
            dialogObj.find("ul:first li:first .btn").addClass("active");
            scope = "全国";
        }

        $("#" + type + "-content li:eq(1)").append('<div class="com-last">' + scope.replace(/,/g, " ; ") + '</div>');
    }
    //
    var setemail = function (mode, email) {
        if (email != "" && email != null) {
            $(".jy_edit span").text(email);
        }
        if (mode == 1) {
            $(".jy_pdwechat img").attr("src", "static/img/on.gif");
            $(".jy_pdemail img").attr("src", "static/img/off.png");
            wochat = 1;
        } else if (mode == 2) {
            $(".jy_pdwechat img").attr("src", "static/img/off.png");
            $(".jy_pdemail img").attr("src", "static/img/on.gif");
            $(".jy_edit").removeClass("hidden");
            $("#s_email").val(email);
            jyemail = 2;
        } else if (mode == 3) {
            $(".jy_pdemail img").attr("src", "static/img/on.gif");
            $(".jy_pdwechat img").attr("src", "static/img/on.gif");
            $(".jy_edit").removeClass("hidden");
            $("#s_email").val(email);
            wochat = 1;
            jyemail = 2;
        }
    }
    //
    $.ajax({
        type: 'post',
        url: '/swordfish/getpage?t=' + new Date().getTime(),
        cache: false,
        dataType: 'json',
        success: function (data) {
            if (data && data.s_type != "") {
                msgset = data.msgset;
                if (msgset == null || msgset == "") {
                    setKeyWord("tender", "");
                    setScope("tender", "");
                    setemail("", "");
                } else {
                    dataObj["keys"] = msgset.a_key
                    dataObj["scope"] = msgset.s_scope
                    setKeyWord("tender", msgset.a_key);
                    setScope("tender", msgset.s_scope);
                    setemail(msgset.i_mode, msgset.s_email)
                }
            } else {
                window.location.href = "/swordfish/share/-1"
            }
        }
    });
    //预览跳转
    //$(".result-view-2").click(function () {
    //    var val = $(this).closest("ul").find("li:first-child>div.com-last").text().replace(/\s+/ig, "")
    //    if (val) {
    //        window.location.href = "/wxpush/bid/wxpushview/" + $(this).attr("v")
    //    } else {
    //        alert("请输入关键字后预览。")
    //    }
    //})

    $(".jy_rssetkw").click(function () {
        window.location.href = "/mobile/filteraddress.php";
    })
    $(".jy_rssetsc").click(function () {
        window.location.href = "/mobile/servicescope.php";
    });
	$(".jy_rssetzb").click(function () {
        window.location.href = "/mobile/filtertender.php";
    });
    //
    var tpfirst = $(".jy_pdemail img").attr("src");
    tpfirst = tpfirst.substr(tpfirst.length - 3);
    if (tpfirst == "gif") {
        $(".jy_edit").removeClass("hidden");
    }
    //
    //$(".email").click();
    $(".jy_edit img").click(function () {
        var s_email = $(".jy_edit span").text();
        $("#s_email").val(s_email);
        $(".error").addClass("vidden");
        $(".email").click();
    });
    //
    $("#sure").click(function () {
        var s_email = $("#s_email").val();
        var s_emailtext = $(".jy_edit span").text();
        if (s_email == s_emailtext) {
            $("#cancel").click();
            return false;
        }
        if (fChkMail(s_email)) {
            $(".error").addClass("vidden");
            $(".jy_edit").removeClass("hidden");
            $(".jy_edit span").text(s_email);
            $(".jy_pdemail img").attr("src", "static/img/on.gif");
            $("#cancel").click();
            jyemail = 2;
            ajaxsave(wochat, jyemail);
        } else {
            $(".error").removeClass("vidden");
        }
    });
    //验证邮箱
    var fChkMail = function (szMail) {
        var szReg = /^([a-zA-Z0-9]+[_|\_|\.|\-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        var bChk = szReg.test(szMail);
        return bChk;
    };

    skipAddress();
});
//
var ajaxsave = function (wc, jm) {
    var email = $(".jy_edit span").text();
    dataObj["s_email"] = email;
    if (wc == 1 && jm == "") {
        dataObj["mode"] = 1;

    } else if (wc == "" && jm == 2) {
        if (email.length == 0) {
            EasyAlert.show("请输入正确的邮箱地址");
        } else {
            dataObj["mode"] = 2;
        }
    } else if (wc == 1 && jm == 2) {
        if (email.length == 0) {
            EasyAlert.show("请输入正确的邮箱地址");
        } else {
            dataObj["mode"] = 3;
        }
    }
    /*****************************************/
    $.ajax({
        type: "POST",
        // url: "/member/swordfish/ajaxReq",
        url: "/mobile/pushset.php",

        data: dataObj,
        dataType: "json",
        traditional: true,
        success: function (r) {
            if (r.flag != "y") {
                EasyAlert.show("操作失败");
            } else if (r.flag == "y") {
                EasyAlert.show("设置成功");
            }
        }
    });

}
//
function remind(obj, we) {
    var tp = $(obj).attr("src");
    tp = tp.substr(tp.length - 3);
    var tpone = "";
    if (we == "w") {
        if (tp == "gif") {
            tpone = $(".jy_pdemail img").attr("src");
            tpone = tpone.substr(tpone.length - 3);
            if (tpone == "png") {
                EasyAlert.show("请保留一种推送方式，<br>合信君还要推送信息给您");
                return;
            } else {
                $(obj).attr("src", "static/img/off.png");
                wochat = "";
            }
        } else if (tp == "png") {
            $(obj).attr("src", "static/img/on.gif");
            wochat = "1";
        }
    } else if (we == "e") {
        if (tp == "gif") {
            $(obj).attr("src", "static/img/off.png");
            $(".jy_pdwechat img").attr("src", "static/img/on.gif");
            $(".jy_edit").addClass("hidden");
            jyemail = "";
            wochat = "1";
        } else if (tp == "png") {
            var email = $(".jy_edit span").text();
            if (email.length == 0) {
                $(".email").click();
                return;
            } else {
                $("#s_email").val(email);
                $(".jy_edit").removeClass("hidden");
                $(".jy_pdemail img").attr("src", "static/img/on.gif");
                jyemail = "2";
            }
        }
    }
    ajaxsave(wochat, jyemail);
}

function skipAddress() {
    $('#divservicescope').bind('click', function () {
        //window.location.href = '<?php ?>';
    });

}
	
	
	
	
	
	
	
	